package com.zyl.simpletextwidget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViews;

public class SimpleTextFullWidgetProvider extends AppWidgetProvider {

 @Override
 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  final int count = appWidgetIds.length;
  
        for (int i = 0; i < count; i++) {
         int appWidgetId=appWidgetIds[i];
            String textPrefix=SimpleTextWidgetConfigure.loadTextPref(context, appWidgetId);
            int textcolor=SimpleTextWidgetConfigure.loadTextColorPref(context, appWidgetId);
       //     int backcolor=SimpleTextWidgetConfigure.loadBackColorPref(context, appWidgetId);
            int textsize=SimpleTextWidgetConfigure.loadTextSizePref(context, appWidgetId);
            int gravity=SimpleTextWidgetConfigure.loadGravityPref(context, appWidgetId);
           
            updateTextAppWidget(context, appWidgetManager, appWidgetId, textPrefix, textcolor,   textsize, gravity);
        }
 }
 
 @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
  final int count = appWidgetIds.length;
        for (int i=0; i<count; i++) {
         SimpleTextWidgetConfigure.deletePref(context, appWidgetIds[i]);
        }
    }


 public static void updateTextAppWidget(Context context, AppWidgetManager appWidgetManager,int appWidgetId,
   String titlePrefix, int textcolor,   int textsize, int gravity) {
  CharSequence text=titlePrefix;
  
     RemoteViews views=new RemoteViews(context.getPackageName(), R.layout.text_widget);
    
     views.setFloat(R.id.widget_text, "setTextSize", SimpleTextWidgetConfigure.getTextSizeInt(textsize));
//     views.setInt(R.id.widget_layout, "setBackgroundColor", setColor(1,backcolor));
     views.setInt(R.id.widget_layout, "setGravity", SimpleTextWidgetConfigure.getGravityInt(gravity));
     views.setTextColor(R.id.widget_text, SimpleTextWidgetConfigure.setColor(2,textcolor));
     views.setTextViewText(R.id.widget_text, text);
   

     Intent intentEdit=new Intent(context,SimpleTextWidgetConfigure.class);
     intentEdit.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     intentEdit.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
     intentEdit.setData(ContentUris.withAppendedId(Uri.EMPTY, appWidgetId));
    
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentEdit, 0);
        views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
    
     appWidgetManager.updateAppWidget(appWidgetId, views);
 }
 
}